package com.alan.controller;

import com.alan.entity.student;
import com.alan.entity.teacherstudent;
import com.alan.repository.StudentRepository;
import com.alan.repository.impl.StudentRepositoryImpl;
import com.alan.service.StudentService;
import com.alan.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private StudentService studentService = new StudentServiceImpl();
    private StudentRepository studentRepository = new StudentRepositoryImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        student student = (student) session.getAttribute("student");
        String method = req.getParameter("method");
        switch (method) {
            case "update":
                req.setAttribute("Pstudent", student.getName());
                req.getRequestDispatcher("/student/Update.jsp").forward(req, resp);
                break;
            case "change":
                req.setAttribute("student", student);
                req.getRequestDispatcher("/student/ChangePassword.jsp").forward(req, resp);
                break;
            case "selfmarks":
                teacherstudent teacherstudent = studentRepository.selfmarks(student.getName());
                req.setAttribute("student", teacherstudent);
                req.getRequestDispatcher("/student/selfmarks.jsp").forward(req, resp);
                break;
            case "quit":
                session.invalidate();
                req.setAttribute("info","退出成功！加油！");
                req.getRequestDispatcher("/Confirm_or_cancel/outsuccess.jsp").forward(req,resp);
                break;


        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method) {
            case "update":
                String stu_id = req.getParameter("stu_id");
                String Cname = req.getParameter("Cname");
                String Ename = req.getParameter("Ename");
                String Ntel = req.getParameter("Ntel");
                studentRepository.update(stu_id, Cname, Ename, Ntel);
                break;
            case "change":
                String name = req.getParameter("name");
                String Npassword = req.getParameter("Npassword");
                System.out.println(name);
                System.out.println(Npassword);
                studentRepository.change(name, Npassword);
                break;

        }

        req.getRequestDispatcher("/student/student.jsp").forward(req, resp);

    }
}
